package com.qingxin.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.cms.entity.CmsArticle;
import com.qingxin.cms.service.CmsArticleService;
import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.WellProduct;
import com.qingxin.park.service.CompanyService;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.service.WellProductService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.DateUtil;


/**
 * 网站首页
 * @author Administrator
 */
@Controller
public class IndexAction extends BaseAction {
	
	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private CmsArticleService cmsArticleService;
	
	@Autowired
	private FarmParkService farmParkService;
	
	@Autowired
	private WellProductService wellProductService;
	
	
	/**
	 * 首页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/index.do", method=RequestMethod.GET)
	public String index(ModelMap model, HttpServletRequest request) {
		model.put("model", "index");
		return "cms/index";
	}
	
	
	/**
	 * 加载首页企业信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/getIndexQiye.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getIndexQiye(HttpServletRequest request){
		List<Map<String, String>> rslist = new ArrayList<>();
		List<Company> list = companyService.listCompanies(3);
		if(list != null){
			for(Company company : list){
				Map<String, String> map = new HashMap<>();
				map.put("companyid", company.getCompanyid());
				map.put("companyName", company.getCompanyName());
				map.put("imgurl", UploadFile.getFileUrl(UploadFile.DIR_COMPANY_IMG, company.getImgurl()));
				map.put("remark", company.getRemark());
				rslist.add(map);
			}
		}
		return rslist;
	}
	
	
	/**
	 * 获取首页文章信息
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/getIndexArticle.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getIndexArticle(String type, HttpServletRequest request){
		List<Map<String, String>> rslist = new ArrayList<>();
		List<CmsArticle> list = cmsArticleService.list(type, 4);
		if(list != null){
			for(CmsArticle article : list){
				Map<String, String> map = new HashMap<>();
				map.put("articleId", article.getArticleId());
				map.put("articleTitle", article.getArticleTitle());
				map.put("thumb", UploadFile.getFileUrl(UploadFile.DIR_NEWS_IMG, article.getThumb()));
				map.put("articleDesc", article.getArticleDesc());
				rslist.add(map);
			}
		}
		return rslist;
	}
	
	
	/**
	 * 获取首页特色园区信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/getIndexPark.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getIndexPark(HttpServletRequest request){
		List<FarmPark> list = farmParkService.listTeseParks(8);
		if(list != null){
			for(FarmPark farmPark : list){
				farmPark.setLogoUrl(UploadFile.getFileUrl(UploadFile.DIR_FARM_IMG, farmPark.getLogoUrl()));
			}
		}else {
			list = new ArrayList<>();
		}
		return list;
	}
	
	
	/**
	 * 获取首页名优产品信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/getIndexProducts.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getIndexProducts(HttpServletRequest request){
		List<Map<String, String>> rslist = new ArrayList<>();
		List<WellProduct> list = wellProductService.listWellProducts(4);
		if(list != null){
			for(WellProduct product : list){
				Map<String, String> map = new HashMap<>();
				map.put("productid", product.getProductid());
				map.put("productName", product.getProductName());
				map.put("parkName", product.getParkName());
				map.put("identificationTime", DateUtil.fmtDate(product.getIdentificationTime()));
				map.put("introduce", product.getIntroduce());
				map.put("productImg", UploadFile.getFileUrl(UploadFile.DIR_WELLPRODUCT_IMG, product.getProductImg()));
				rslist.add(map);
			}
		}
		return rslist;
	}
	
	
	
	
	
	
}
